import 'package:flutter/material.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/pages/game/game_detail/game_detail_logic.dart';
import 'package:flutter_09_game/pages/game/game_detail/widgets/widget_odd_even_layout.dart';
import 'package:flutter_09_game/pages/game/game_detail/widgets/widget_round_ball.dart';

import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

/// 走势图组件
class WidgetTrendChart extends StatelessWidget {
  /// 走势图数据

  final GameDetailLogic gameLogic;
  final ScrollController scrollController;
  const WidgetTrendChart({
    super.key,
    required this.gameLogic,
    required this.scrollController,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
        color: AppColor.navBgColor,
        // height:  182.w,
        // decoration: BoxDecoration(
        //   color: AppColor.navBgColor, // 深色背景
        //   borderRadius: BorderRadius.circular(12.w),
        // ),
        padding: EdgeInsets.all(12.w),
        child: Obx(() {
          return Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              _buildTitle(),
              8.verticalSpaceFromWidth,
              Container(
                decoration: BoxDecoration(
                    color: AppColor.pageBgColor,
                    borderRadius: BorderRadius.circular(12.w)),
                padding: EdgeInsets.all(8.w).copyWith(left: 0),

                child: WidgetOddEvenLayout(
                  historyData: gameLogic.gameRoundRx.value.history ?? [],
                  isBig: false, // 使用小球样式
                  maxItemsPerColumn: 5,
                  scrollController: scrollController,
                ),
              ),
            ],
          );
        }));
  }

  /// 构建标题
  Widget _buildTitle() {
    final counts = _calculateOddEvenCounts();
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          LocaleKeys.blockTrend.tr,
          style: TextStyle(
            fontSize: 16.sp,
            fontWeight: FontWeight.w600,
            color: AppColor.textPrimary.withOpacity(0.94),
          ),
        ),
        12.horizontalSpace,
        // 单双球示例
        // _buildLegend(),
        _buildLegendItem(isOdd: true, value: counts['odd'] ?? 0),
        4.horizontalSpace,
        _buildLegendItem(isOdd: false, value: counts['even'] ?? 0),
      ],
    );
  }

  /// 计算单双数个数
  Map<String, int> _calculateOddEvenCounts() {
    final history = gameLogic.gameRoundRx.value.history ?? [];
    int oddCount = 0;
    int evenCount = 0;

    for (var item in history) {
      final result = item.result ?? 0;
      if (result % 2 == 1) {
        oddCount++;
      } else {
        evenCount++;
      }
    }

    return {
      'odd': oddCount,
      'even': evenCount,
    };
  }

  /// 构建图例
  // Widget _buildLegend() {
  //   final counts = _calculateOddEvenCounts();
  //
  //   return Row(
  //     mainAxisSize: MainAxisSize.min,
  //     children: [
  //       _buildLegendItem(isOdd: true, value: counts['odd'] ?? 0),
  //       4.horizontalSpace,
  //       _buildLegendItem(isOdd: false, value: counts['even'] ?? 0),
  //     ],
  //   );
  // }

  /// 构建图例项
  Widget _buildLegendItem({required bool isOdd, required int value}) {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        WidgetRoundBall(
          result: isOdd ? 1 : 0,
          isBig: true,
          // size: 20.w,
        ),
        4.horizontalSpace,
        Text(
          value.toString(),
          style: TextStyle(
            fontSize: 14.sp,
            fontWeight: FontWeight.w600,
            color: isOdd
                ? AppColor.homePrizePoolIndicator
                : AppColor.gameResultEven,
          ),
        ),
      ],
    );
  }
}
